//
//  ChooseProClassVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2024/11/5.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
import EmptyDataSet_Swift
let  baseClaWidth = 38
class ChooseProClassVC: ZyaChooseProductVC {
    var dataArray = [ZyaFOGoodType]()
    var itemArray = [ZyaFOGoodTPre]()
    
    var selectMark:[Int?] = [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
    var selectDeepIndex = -1

    func arrreset() {
        selectMark = [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
        selectDeepIndex = -1
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.network()
        }
   
        self.configClassification()
        self.tableView.mj_header?.beginRefreshing()
        
        
    }
    override func network() {
        
        var api:API!
        
        KSNetwork.shared.doRequest(API.pFoodQueryTypeAndSecType(s_org_id: s_orgid),showHud: false) { (data:BaseResult<ZyaFOGoodType>) in
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    self.dataArray = infos
                    self.arrreset()
                    self.tableView.reloadData()
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
        api = API.pFoodQueryGoodsByType(s_org_id: s_orgid, s_client_id: clientid,s_customer_branch_id: s_customer_branch_id,s_date: s_date)
        KSNetwork.shared.doRequest(api,showHud: false) { (data:BaseResult<ZyaFOGoodTPre>) in
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    
                    let newInfos = infos.compactMap { one -> ZyaFOGoodTPre in
                        var nn = one
                        return nn
                    }
                    
                    self.itemArray = newInfos
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
            
            self.tableView.mj_header?.endRefreshing()
        }
        
        
    }
    
    func configClassification() {
 
        claBgCollectionV.selectItemClo = { inp in
            if inp.row == 0 {
                self.arrreset()
                self.configZyaClaBtnView()
                self.tableView.reloadData()
                
                
            } else if inp.row == 1 {
                self.selectMark[1] = nil
                self.selectDeepIndex = -1
                self.configZyaClaBtnView()
                self.tableView.reloadData()
            } else if inp.row == 2 {
                self.configZyaClaBtnView()
                self.tableView.reloadData()
            }
            
        }
        self.tableView.tableHeaderView = claBgCollectionV
        self.configZyaClaBtnView()
    }
    func configZyaClaBtnView() {
      
        var titles = [String]()
        var widths = [Int]()
        let t1 = "全部"
        titles.append("全部")
        let w1 = baseClaWidth + t1.count * 12
        widths.append(w1)
   
        
        if selectMark[0].isSome {
            let t2 = self.dataArray[selectMark[0]!].s_type_name.unwrap
            titles.append(t2)
            
            let w2 = baseClaWidth + t2.count * 12
            widths.append(w2)
        
            if self.selectMark[1].isSome {
                let t3 = self.dataArray[self.selectMark[0]!].s_sec?[self.selectMark[1]!].s_sec_type_name ?? ""
                let w3 = baseClaWidth + t3.count * 12
                titles.append(t3)
                widths.append(w3)
             
            }
        }
        claBgCollectionV.dataArray = titles
        claBgCollectionV.maxlengths = widths

        
        claBgCollectionV.collectionView.reloadData()
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaChooseProCell.identifier, for: indexPath) as? ZyaChooseProCell else {
            fatalError()
        }
        
        
        if self.selectMark[0].isNone {
            
            cell.setSuperType(with: self.dataArray[indexPath.row].s_type_name.unwrap)
            
            
        } else if self.selectMark[1].isNone {
            cell.setSuperType(with: self.dataArray[self.selectMark[0]!].s_sec![indexPath.row].s_sec_type_name.unwrap)
            
        } else  {
            if let secCode = self.dataArray[self.selectMark[0]!].s_sec![self.selectMark[1]!].s_sec_type_code {
                let arr = self.itemArray.filter { $0.s_sec_type_code == secCode }
                cell.setLastNodeType(with: arr.first!.goods![indexPath.row])
            }
        }
        
        
        // 点击收藏 事件
        cell.markClosure = { [indexPath] in
            
            
            if let secCode = self.dataArray[self.self.selectMark[0]!].s_sec![self.self.selectMark[1]!].s_sec_type_code {
                if let index = self.itemArray.getIndexFor({ one in
                    one.s_sec_type_code == secCode
                }) {
                    let sm = self.itemArray[index].goods![indexPath.row].sMark.unwrap
                    self.itemArray[index].goods![indexPath.row].sMark = (sm == "1") ? "0" : "1"
                    self.setMarkNetwork(id: self.itemArray[index].goods![indexPath.row].sGoodsId.unwrap, mark: self.itemArray[index].goods![indexPath.row].sMark.unwrap)
                    self.tableView.reloadData()
                }
            }
            
            
            
        }
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if self.selectMark[0].isNone {
            return self.dataArray.count
        } else if self.selectMark[1].isNone {
            if let sec = self.dataArray[self.selectMark[0]!].s_sec {
                return sec.count
            } else {
                return 0
            }
        }  else  {
            if let secCode = self.dataArray[self.selectMark[0]!].s_sec![self.selectMark[1]!].s_sec_type_code {
                let arr = self.itemArray.filter { $0.s_sec_type_code == secCode }
                return arr.first?.goods?.count ?? 0
            } else {
                return 0
            }
        }
        
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
        if self.self.selectMark[0].isNone {
            self.self.selectMark[0] = indexPath.row
            self.tableView.reloadData()
            self.configZyaClaBtnView()
        } else if self.self.selectMark[1].isNone {
            self.self.selectMark[1] = indexPath.row
            self.tableView.reloadData()
            self.configZyaClaBtnView()
        } else   {
            if let secCode = self.dataArray[self.selectMark[0]!].s_sec![self.selectMark[1]!].s_sec_type_code {
                if let index = self.itemArray.getIndexFor({ one in
                    one.s_sec_type_code == secCode
                }) {
                    self.itemArray[index].goods![indexPath.row].selectX =  !self.itemArray[index].goods![indexPath.row].selectX
                    self.tableView.reloadData()
                }
            }
        }
        
        
        
    }
    
}
